package negocio;

import java.sql.SQLException;
import java.util.Iterator;

import repositorio.ConexaoBD;
import repositorio.RepositorioCentral;
import dados.Central;
import excecoes.CentralJaExisteException;
import excecoes.CentralNaoExisteException;

public class ControladorCentral {

	private RepositorioCentral repositorioCentral;

	public ControladorCentral(ConexaoBD conexao){
		this.repositorioCentral = new RepositorioCentral(conexao);
	}

	public void cadastrar(Central central) throws SQLException, CentralJaExisteException{
		boolean existe = this.existe(central);

		if(!existe){
			this.repositorioCentral.inserir(central);
		}
		else{
			throw new CentralJaExisteException();
		}
	}

	public void editar(Central central) throws SQLException{
		this.repositorioCentral.editar(central);
	}

	public void remover(Central central) throws SQLException{
		this.repositorioCentral.remover(central.getId());
	}
	
	public Central buscar(Central central) throws CentralNaoExisteException, SQLException{
		Central temp = this.repositorioCentral.buscar(central.getId());
		
		if(temp != null){
			return temp;
		}
		else{
			throw new CentralNaoExisteException();
		}
	}

	public Iterator<Central> getIterator() throws SQLException{
		return this.repositorioCentral.iterator();
	}
	
	public boolean existe(Central central) throws SQLException{
		boolean retorno = false;

		Central temp = this.repositorioCentral.buscar(central.getId());

		if(temp != null){
			retorno = true;
		}

		return retorno;
	}
}
